package com.park.web.controller.user;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.park.client.model.article.ArticleTypeModel;
import com.park.client.model.resource.ResourceCollectionModel;
import com.park.client.model.resource.ResourceDownloadRecordModel;
import com.park.client.model.resource.ResourceFileModel;
import com.park.client.model.user.UserModel;
import com.park.client.service.article.ArticleTypeService;
import com.park.client.service.resource.ResouceFileService;
import com.park.client.service.resource.ResourceCollectionService;
import com.park.client.service.resource.ResourceDownloadRecordService;
import com.park.common.file.FileUtil;
import com.park.web.contant.Global;
import com.park.web.controller.BaseController;
import com.park.web.util.SessionUtil;

@Controller
@RequestMapping("/user/resource")
public class UserResourceFileController extends BaseController{

	@Autowired
	private ResouceFileService resouceFileService;
	@Autowired
	private ResourceDownloadRecordService downloadRecordService;
	@Autowired
	private ResourceCollectionService resourceCollectionService;
	@Autowired
	private ArticleTypeService articleTypeService;
	/**
	 *个人资源显示列表页面
	 */
	@RequestMapping("resourceManage")
	public String resourceList(HttpServletRequest request,HttpSession session){
		UserModel user = SessionUtil.getSessionUser(session);
		List<ResourceFileModel> resList = resouceFileService.selectByUserId(user.getId());
		request.setAttribute("resList", resList);
		return Global.USER_PATH+"resourcefile/resourceManage";
	}
	
	/**
	 *上传资源页面
	 */
	@RequestMapping("toUploadResFile")
	public String toUploadResFile(HttpServletRequest request){
		return Global.USER_PATH+"resourcefile/uploadResouce";
	}
	
	/**
	 * 文章类型跟资源类型一样
	 * @return
	 */
	
	@RequestMapping("listType")
	@ResponseBody
	public List<ArticleTypeModel> listType(HttpServletRequest request,HttpSession session){
		List<ArticleTypeModel> articleTypes = articleTypeService.selectAll();
		return articleTypes;
	}
	
	/**
	 *上传资源功能
	 */
	@RequestMapping("uploadResFile")
	public String uploadResFile(@RequestParam("file") MultipartFile file,ResourceFileModel rf,HttpServletRequest request,HttpSession session){
		if(!file.isEmpty()){
			try {
				String fileName = file.getOriginalFilename();
				int index = fileName.lastIndexOf(".");
				String size = FileUtil.getFileSize(file.getSize());
				UserModel user = SessionUtil.getSessionUser(session);
				String filePath = FileUtil.getResourceFilePath(request,user.getUsername(),fileName);
				file.transferTo(new File(filePath));
				rf.setFileType(fileName.substring(index+1));
				rf.setUploadDate(new Date());
				rf.setCreater(user.getId());
				rf.setResourceSize(size);
				rf.setIsPass(0);
				rf.setFilePath(filePath);
//				rf.setResourceFileContent(FileUtil.getBytes(is));
				resouceFileService.insertSelective(rf);	
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return "redirect:resourceManage";
	}
	
	/**
	 *个人资源删除功能
	 */
	@RequestMapping("delResource")
	public String delResource(@RequestParam("id") Long id,HttpServletRequest request,HttpSession session){
		resouceFileService.deleteByPrimaryKey(id);
		return "redirect:resourceManage";
	}
	
	
	/**
	 *个人下载资源列表页面
	 */
	@RequestMapping("downloadRecordList")
	public String downloadRecordList(HttpServletRequest request,HttpSession session){
		UserModel user = SessionUtil.getSessionUser(session);
		List<ResourceDownloadRecordModel> rdr = downloadRecordService.selectByUserId(user.getId());
		List<ResourceFileModel> resList = new ArrayList<ResourceFileModel>();
		for (ResourceDownloadRecordModel r : rdr) {
			ResourceFileModel rf = resouceFileService.selectByPrimaryKey(r.getResourceId());
			resList.add(rf);
		}
		request.setAttribute("resList", resList);
		return Global.USER_PATH+"resourcefile/resourceDownloadRecordList";
	}
	
	/**
	 *个人收藏资源列表页面
	 */
	@RequestMapping("resourceCollectionList")
	public String resourceCollectionList(HttpServletRequest request,HttpSession session){
		UserModel user = SessionUtil.getSessionUser(session);
		List<ResourceCollectionModel> rdr = resourceCollectionService.selectByUserId(user.getId());
		List<ResourceFileModel> resList = new ArrayList<ResourceFileModel>();
		for (ResourceCollectionModel r : rdr) {
			ResourceFileModel rf = resouceFileService.selectByPrimaryKey(r.getResourceId());
			resList.add(rf);
		}
		request.setAttribute("resList", resList);
		return Global.USER_PATH+"resourcefile/resourceCollectionList";
	}
	
	/**
	 * 取消收藏
	 */
	@RequestMapping("cancelCollection")
	public String cancelCollection(Long resId,HttpServletRequest request,HttpSession session){
		UserModel user = SessionUtil.getSessionUser(session);
		ResourceCollectionModel record = new ResourceCollectionModel();
		record.setUserId(user.getId());
		record.setResourceId(resId);
		resourceCollectionService.cancelResource(record);
		return  "redirect:resourceCollectionList";
	}
}
